<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" size="small" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存当前选中的角色列表
      list: []
    }
  },
  created() {
    //   组件创建的时候执行一次
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.roleIds = []
      this.$emit('close-dialog')
    },
    async loadRoles() {
      const { data } = await getRoles({ page: 1, pagesize: 100 })
      const info = await getUserDetailById(this.employeeId)
      //   保存这个员工当前已经有的角色·
      this.roleIds = info.data.roleIds
      //   保存所有角色
      this.list = data.rows
    },
    // 保存当前角色
    async hSave() {
      const res = await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      console.log('保存当前角色信息', res)
      //   提示
      this.$message.success('分配角色成功')
      // 关闭弹层
      this.$emit('update-close')
    }
  }
}
</script>
